<template>
  <!-- eslint-disable -->
  <div class="studentArr">
    <div class="topArea">
      <div class="inputSearch">
        <el-input v-model="studentParams.s" placeholder="请输入学生学号或姓名或手机号搜索" style="width: 280px;" prefix-icon="el-icon-search" clearable @keyup.enter.native="handleSearchTable" />
      </div>
      <div class="topBtnGroup">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleEnterAddStudent">新增学生</el-button>
        <!-- <el-button type="primary" size="small" icon="el-icon-s-operation">批量导入</el-button> -->
      </div>
    </div>
    <div class="middleArea">
      <el-radio-group v-model="studentParams.active" @change="handleChoStatus">
        <el-radio-button :label="null">全部</el-radio-button>
        <el-radio-button :label="1">未激活</el-radio-button>
        <el-radio-button :label="2">已激活</el-radio-button>
      </el-radio-group>

      <div class="filter-container">
        <i class="el-icon-refresh-left refresh" @click="handleClearScreening"></i>
        <el-select v-model="studentParams.department" placeholder="请选择院系" clearable class="inputWidth">
          <el-option
            v-for="item in departmentsList"
            :key="item.key"
            :label="item.value"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-cascader
          v-model="searchMajor"
          placeholder="请选择专业"
          :options="majorsList"
          :props="defaultProps"
          class="inputWidth"
          clearable
          @change="handleCascaderChange"
        />
        <el-select v-model="studentParams.start_year" placeholder="请选择年级" clearable class="inputWidth">
          <el-option
            v-for="item in gradeList"
            :key="item.key"
            :label="item.value"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button style="width:100px;margin-bottom: 20px;" @click="handleScreeningChange">筛选</el-button>
      </div>
    </div>

    <!-- 列表 -->
    <el-table :data="studentList" v-loading="loading" style="width: 100%">
      <el-table-column align="center" width="200" prop="no" label="学号" />
      <el-table-column align="center" prop="name" label="姓名" />
      <el-table-column align="center" prop="gender" label="性别">
        <template slot-scope="{row}">
          <span>{{ row.gender === 1 ? '男' : '女' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="mobile" label="绑定手机号" />
      <el-table-column align="center" prop="academy_department" label="所属院系" />
      <el-table-column align="center" prop="academy_major" label="所学专业" />
      <el-table-column align="center" prop="start_year" label="年级" />
      <el-table-column align="center" prop="is_active" label="激活状态">
        <template slot-scope="{row}">
          <span>{{ row.is_active === 1 ? '未激活' : '已激活' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="last_login" label="登录时间">
        <template slot-scope="{row}">{{ row.last_login ? row.last_login : '/' }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <el-tooltip effect="dark" content="编辑" placement="top-start">
            <span class="btnIcon edit" @click="handleEditStudent(row)"></span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange" 
      @current-change="handleCurrentChange" 
      :current-page.sync="studentParams.page" 
      :page-sizes="[10,20,30,50]" 
      :page-size="studentParams.limit" 
      layout="prev, pager, next, total, sizes, jumper"
      :total="total" 
      style="margin-top: 30px;"
    />
  </div>
</template>

<script>
import { getDepartments, getMajors, getYears, getStudents } from '../../../api/student'
export default {
  data() {
    return {
      screening: { // 筛选条件
        department: '',
        major: '',
        grade: ''
      },
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'name'
      },
      departmentsList: [], // 院系列表
      majorsList: [], // 专业列表
      gradeList: [], // 年份列表
      searchMajor: [],
      loading: false,
      studentParams: {
        page: 1,
        limit: 10,
        s: '',
        active: null, // 是否激活状态   null--全部   1--已激活  2--未激活
        department: null, // 院系
        catalog: null, // 专业大类
        major: null, // 专业
        start_year: null // 入学年份/年级
      },
      studentList: [],
      total: null
    }
  },
  mounted() {
    this.getDepartments()
    this.getMajors()
    this.getYears()
    this.getStudentsList()
  },
  methods: {
    // 获取院系列表
    getDepartments() {
      getDepartments().then(res => {
        this.departmentsList = res
      })
    },
    // 获取专业列表
    getMajors() {
      getMajors().then(res => {
        this.majorsList = res
      })
    },
    // 获取年份
    getYears() {
      getYears({
        start: 1990,
        step: 30
      }).then(res => {
        this.gradeList = res
      })
    },
    // 获取学生列表
    getStudentsList() {
      this.loading = true
      getStudents(this.studentParams).then(res => {
        this.studentList = res.data
        this.total = res.meta.total
      }).finally(() => {
        this.loading = false
      })
    },
    handleChoStatus() {
      this.studentParams.page = 1
      this.studentParams.limit = 10
      this.getStudentsList()
    },
    // 分页
    handleSizeChange(val) {
      this.studentParams.limit = val
      this.getStudentsList()
    },
    handleCurrentChange(val) {
      this.studentParams.page = val
      this.getStudentsList()
    },
    // 点击'新增学生'按钮
    handleEnterAddStudent() {
      this.$router.push({ path: './add' })
    },
    handleEditStudent(row) {
      this.$router.push({
        path: './edit',
        query: {
          id: row.id
        }
      })
    },
    // 点击'筛选'按钮
    handleScreeningChange() {
      this.studentParams.page = 1
      this.studentParams.limit = 10
      this.studentParams.active = null
      this.getStudentsList()
    },
    // 点击'清空'按钮
    handleClearScreening() {
      this.studentParams.department = null
      this.studentParams.catalog = null
      this.studentParams.major = null
      this.studentParams.start_year = null
      this.searchMajor = []
    },
    // 点击'搜索'按钮
    handleSearchTable() {
      this.studentParams.page = 1
      this.studentParams.limit = 10
      this.studentParams.department = null
      this.studentParams.catalog = null
      this.studentParams.major = null
      this.studentParams.start_year = null
      this.studentParams.active = null
      this.searchMajor = []
      this.getStudentsList()
    },
    handleCascaderChange(value) {
      if (value) {
        this.studentParams.catalog = value[0]
        this.studentParams.major = value[1]
      }
    }
  }
}
</script>

<style lang="scss">
  $defaultColor: #0A2045;
  .studentArr {
    padding: 30px;
    .topArea {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      .el-input--suffix .el-input__inner {
        padding-right: 0;
      }
    }
    .middleArea {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      .el-radio-button__orig-radio:checked+.el-radio-button__inner {
        background-color: #F1F7FF;
        border: none;
        color: #0C66FF;
        box-shadow: none;
      }
      .el-radio-button__inner {
        border: none;
        border-radius: 8px;
        color: #6C798F;
      }
      .el-radio-group {
        border: 1px solid #E1E4EB;
        border-radius: 8px;
      }

      .filter-container {
        .refresh {
          color: #B2B7C2;
          font-size: 20px;
          margin-right: 10px;
          cursor: pointer;
        }
        .inputWidth {
          margin-right: 10px;
          width: 224px;
          height: 38px;
          line-height: 38px;
        }
      }
    }
  }
  .btnIcon {
    display: inline-block;
    width: 24px;
    height: 24px;
    cursor: pointer;
    margin-right: 10px;
  }
  // 通过
  .edit {
    background: url('../../../assets/btn/icon_edit.png') no-repeat center center;
  }
  .edit:hover {
    background: url('../../../assets/btn/icon_edit_s.png') no-repeat center center;
  }

  // 标题头样式修改
  .el-table {
    color: $defaultColor;
    font-size: 14px;
    line-height: 19px;
    thead {
      color: $defaultColor;
      tr, th {
        background: #FAFAFB;
      }
    }
  }
  .el-button:focus, .el-button:hover {
    color: #0C66FF;
    border-color: #0C66FF;
    background-color: #fff;
  }
  .el-button--primary {
    color: #FFF;
    background-color: #0C66FF;
    border-color: #0C66FF;
  }
  .el-button--small {
    border-radius: 8px;
  }
  .el-button--primary:hover {
    color: #fff;
    background-color: #0951CC;
    border-color: #0951CC;
  }
</style>
